#include "unique_filename.hpp"
#include <boost/uuid/uuid_generators.hpp>
#include <boost/filesystem.hpp>
#include <string>
#include <sstream>

//#define __UNITTEST__

UniqueFileName::UniqueFileName ()
: seed ( boost::uuids::random_generator ()() )
{

}

UniqueFileName::~UniqueFileName ()
{

}

void UniqueFileName::get_fname ( string& fname )
{
	stringstream ss;

refresh:
	ss << seed;
	string filename = ss.str();

	boost::filesystem::path cpath = boost::filesystem::current_path();
	boost::filesystem::path fpath = cpath / filename;

	if ( boost::filesystem::exists ( fpath ) )
	{
		refresh ();
		goto refresh;
	}

	fname = fpath.string();

}

void UniqueFileName::refresh ()
{
	seed = boost::uuids::random_generator ()();
}

#if (defined __UNITTEST__)

int main ( int argc, char* argv[] )
{

	UniqueFileName fname;
	string filename;

	fname.get_fname ( filename );
	cout << filename << "\n" << endl;

	fname.refresh ();
	fname.get_fname ( filename );
	cout << filename << "\n" << endl;

	fname.refresh ();
	fname.get_fname ( filename );
	cout << filename << "\n" << endl;

	fname.refresh ();
	fname.get_fname ( filename );
	cout << filename << "\n" << endl;

	fname.refresh ();
	fname.get_fname ( filename );
	cout << filename << "\n" << endl;

	return 0;
}

#endif